﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Namer.Pages
{
    public class SettingPage
    {
        private static readonly List<Control> PageControls = new List<Control>();
        public SettingPage() => Interlize();

        public List<Control> GetControls(int x)
        {
            PageControls.Clear();
            Interlize();
            foreach (Control ctr in PageControls) ctr.Location = new Point(x + ctr.Location.X, ctr.Location.Y);
            return PageControls;
        }

        private static UIListBox Detil;
        private static UIListBox users;
        private static UITextBox tbx1;

        private static void Interlize()
        {
            UIMarkLabel title = new UIMarkLabel()
            {
                Text = "设置",
                Location = new Point(10, 10),
                Height = 25
            }; PageControls.Add(title);
            UITabControl tb1 = new UITabControl()
            {
                Location = new Point(10,40),
                Style = UIStyle.LayuiOrange,
                Size = new Size(UI.MainForm.Width - 65,UI.MainForm.Height - 80),
                TabSelectedColor = Color.FromArgb(100,51, 51, 51)
            };PageControls.Add(tb1);
            TabPage General = new TabPage()
            {
                Text = "通用"
            };tb1.Controls.Add(General);
            UIMarkLabel m1 = new UIMarkLabel()
            {
                Text = "更新程序",
                Location = new Point(10,10)
            };General.Controls.Add(m1);
            UIButton btn = new UIButton()
            {
                Text = "检查更新",
                Location = new Point(General.Width - 100,10),
                Width = 90,
                Height = 30,
                Style = UIStyle.LayuiGreen
            };General.Controls.Add(btn); btn.Click += Btn_Click;

            TabPage USers = new TabPage() {Text = "人员管理"};tb1.Controls.Add(USers);
            users = new UIListBox()
            {
                Location = new Point(5,5),
                Height = USers.Height - 10,
                Width = 150
            };USers.Controls.Add(users);
            UIButton edit = new UIButton()
            {
                Location = new Point(160,5),
                Size = new Size(35,35),
                Text = "\ue70f",
                Style = UIStyle.Purple,
                Font = new Font("Segoe MDL2 Assets",13)
            };USers.Controls.Add(edit); edit.Click += Edit_Click;
            UIButton newa = new UIButton()
            {
                Location = new Point(160, 45),
                Size = new Size(35, 35),
                Text = "\ue710",
                Style = UIStyle.Green,
                Font = new Font("Segoe MDL2 Assets", 13)
            }; USers.Controls.Add(newa); newa.Click += Newa_Click;
            UIButton delete = new UIButton()
            {
                Location = new Point(160, 85),
                Size = new Size(35, 35),
                Text = "\ue711",
                Style = UIStyle.Red,
                Font = new Font("Segoe MDL2 Assets", 13)
            }; USers.Controls.Add(delete); delete.Click += Delete_Click;
            Detil = new UIListBox()
            {
                Location = new Point(200, 5),
                Height = USers.Height - 50,
                Width = USers.Width - 210
            }; USers.Controls.Add(Detil);
           tbx1 = new UITextBox()
            {
                Location = new Point(200, USers.Height - 40),
                Height = 35,
                Width = Detil.Width - 40
            };USers.Controls.Add(tbx1);
            UIButton sure = new UIButton()
            {
                Location = new Point(205 + tbx1.Width, USers.Height - 40),
                Size = new Size(35, 35),
                Text = "\ue8fb",
                Style = UIStyle.Orange,
                Font = new Font("Segoe MDL2 Assets", 13)
            }; USers.Controls.Add(sure);OnSure(); sure.Click += Sure_Click;
            UIButton help = new UIButton()
            {
                Location = new Point(160 , USers.Height - 40),
                Size = new Size(35, 35),
                Text = "\ue897",
                Style = UIStyle.LayuiOrange,
                Font = new Font("Segoe MDL2 Assets", 13)
            }; USers.Controls.Add(help); help.Click += Help_Click;
        }
        public static string Version { get; } = "1.0";
        public static string Date { get; } = "20220518";
        private static void Btn_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient wc = new WebClient();
                JObject root = JObject.Parse(Encoding.UTF8.GetString(wc.DownloadData("https://fbtstudio.gitee.io/webframwork/update/UpdateName.json")));
                if (!root["version"].ToString().Equals(Version) || !root["date"].ToString().Equals(Date))
                {
                    MessageBox.Show("有新版本!开始更新ing");
                    wc.DownloadFile(root["url"].ToString(), "data.update");
                    Directory.CreateDirectory("data");
                    ZipFile.ExtractToDirectory("data.update", "data");
                    Process.Start("data\\Update.exe");
                } else MessageBox.Show("太好了,您的是最新版本!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch(Exception) { MessageBox.Show("啊哦,您好像没有连接网络", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
        }

        private static void Help_Click(object sender, EventArgs e)
        {
            MessageBox.Show("");
        }

        private static void Delete_Click(object sender, EventArgs e)
        {
            bool back = UI.MainForm.ShowAskDialog("真的要删除吗", true);
            if (back) BackGround.Delete(users.SelectedItem.ToString());
            OnSure();
        }

        private static void Newa_Click(object sender, EventArgs e)
        {
            string data = "";
            bool istrue = UIInputDialog.InputStringDialog(UI.MainForm,ref data,true,"输入新的班级名称",true);
            if (istrue)
            {
                BackGround.CreatNew(data, new string[] { "默认" });
                MessageBox.Show("创建成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
                OnSure();
            }
        }

        private static void Sure_Click(object sender, EventArgs e)
        {
            try
            {
                int detil = Detil.SelectedIndex;
                BackGround.SaveToFile(users.SelectedItem.ToString(), tbx1.Text, detil);
                MessageBox.Show("成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            catch
            {
                BackGround.AddToFileEnd(users.SelectedItem.ToString(), tbx1.Text);
                MessageBox.Show("成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            string[] data = BackGround.UsersList[users.SelectedItem.ToString()];
            Detil.Items.Clear();
            foreach (string usern in data) Detil.Items.Add(usern);
        }

        private static void Edit_Click(object sender, EventArgs e)
        {
            string[] data = BackGround.UsersList[users.SelectedItem.ToString()];
            Detil.Items.Clear();
            foreach (string usern in data) Detil.Items.Add(usern);
            MessageBox.Show("选中项目成功!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private static void OnSure()
        {
            users.Items.Clear();
            foreach (string clsname in BackGround.cm) users.Items.Add(clsname);
            if (users.Items.Count > 0) users.SelectedIndex = 0; 
        }
    }
}
